//
//  Global.m
//  omni
//
//  Created by Phuong Dinh Le on 1/25/13.
//  Copyright (c) 2013 StarPound. All rights reserved.
//

#import "Global.h"

#define MAXLENGTH_LOG_STRING 8000
NSString const *logLevels[] = { @"Off", @"Error", @"Warning", @"Info", @"Debug" };

void NSLogWithLevel(NSUInteger logLevel, NSString *format, ...)
{
#ifndef __NDEBUG__
    if ( logLevel > __LOG_LEVEL__ )
    {
        return;
    }
    
    __block va_list arg_list;
    va_start (arg_list, format);
    
    NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:arg_list];
    
    va_end(arg_list);
    
    if (formattedString.length <= MAXLENGTH_LOG_STRING)
    {
        NSLog(@"[%@] %@", logLevels[logLevel], formattedString);
    }
    else
    {
        NSRange range;
        range.location = 0;
        range.length = MAXLENGTH_LOG_STRING;
        
        NSLog(@"[%@] %@", logLevels[logLevel], [formattedString substringWithRange:range]);
        range.location += range.length;
        
        while (range.location< formattedString.length)
        {
            if (range.location + range.length < formattedString.length)
            {
                NSLog(@"%@",[formattedString substringWithRange:range]);
            }
            else
            {
                NSLog(@"%@",[formattedString substringFromIndex:range.location]);
                break;
            }
            
            range.location += range.length;
        }
    }
#endif
}
